package com.itheima.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;

/**
 * @program: itheima-jdk8
 * @description:
 * @author: zhanghz001
 * @create: 2021-01-09 10:16
 **/
public class ThenCombineDemo1 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行子线程1");
            System.out.println(Thread.currentThread().getName());
            
            return 111;
        }, pool);
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行子线程2");
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 222;
        }, pool);
        // CompletableFuture<Object> async = future1.thenCombineAsync(future2, new BiFunction<Integer, Integer, Object>() {
        //     @Override
        //     public Object apply(Integer integer, Integer integer2) {
        //         System.out.println(Thread.currentThread().getName());
        //         return null;
        //     }
        // }, pool);
        // try {
        //     async.get();
        // } catch (InterruptedException e) {
        //     async.cancel(true);
        //     e.printStackTrace();
        // } catch (ExecutionException e) {
        //     async.cancel(true);
        //    
        //     e.printStackTrace();
        // } finally {
        //     pool.shutdown();
        // }
        
        //
        // CompletableFuture<Void> future2 = future.thenAcceptBothAsync(future1, new BiConsumer<Integer, Integer>() {
        //     @Override
        //     public void accept(Integer integer, Integer integer2) {
        //         System.out.println(integer);
        //         System.out.println(integer2);
        //         System.out.println(Thread.currentThread().getName());
        //     }
        // }, pool);
        //
        // //
        // try {
        //     future2.get();
        // } catch (InterruptedException e) {
        //     future2.cancel(true);
        //     e.printStackTrace();
        // } catch (ExecutionException e) {
        //     future2.cancel(true);
        //    
        //     e.printStackTrace();
        // } finally {
        //     pool.shutdown();
        // }
        
        //
        // future.runAfterBothAsync(future1, new Runnable() {
        //     @Override
        //     public void run() {
        //         System.out.println(Thread.currentThread().getName());
        //     }
        // }, pool);
        // try {
        //     future.get();
        // } catch (InterruptedException e) {
        //     future.cancel(true);
        //     e.printStackTrace();
        // } catch (ExecutionException e) {
        //     future.cancel(true);
        //     e.printStackTrace();
        // } finally {
        //     pool.shutdown();
        // }
        
        //
        
        CompletableFuture<Object> apply = future.applyToEitherAsync(future1, new Function<Integer, Object>() {
            @Override
            public Object apply(Integer integer) {
                System.out.println(Thread.currentThread().getName());
                System.out.println(integer);
                return null;
            }
        }, pool);
        try {
            apply.get();
        } catch (InterruptedException e) {
            apply.cancel(true);
            e.printStackTrace();
        } catch (ExecutionException e) {
            apply.cancel(true);
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
    }
    
}
